import { ref, type Ref, onMounted } from 'vue'
import { RequestGoodsById } from '@/apis/detail'
import type { IGoods,ICartGoods } from '@/types/type'
// 传参
import { useRouter } from 'vue-router'
// 获取id
import { useRoute } from 'vue-router'
import { Toast } from 'vant';
import { getcollageStatus } from '@/stores/collage'
// import { payment } from '@/stores/payment'

export const getGroupsDetails = () => {
    // 退出到拼团页面
    const onClickLeft = () => history.back()
    const onviewDetails = () => history.back()
    const onviewOrder = () => history.back()
    // 商品详情  用对象接收
    const groupGoods: Ref<IGoods> = ref({
        id: 0,
        shop: '',
        picture: '',
        product: '',
        price: 0,
        num: 0,
        oldprice: 0,
        putaway: 0,
        detail: '',
        categoryname: '',
    })
    const Route = useRoute()
    const id: any = Route.query.id//接收home传过来的参数   

    // 点击邀请好友 跳转到邀请好友页面
    const currentGoods = async (id:number) => {
        const data = await RequestGoodsById(id)
        const { resultCode, resultInfo } = data
        if (resultCode === 1) {
            groupGoods.value = resultInfo
        }
    }
    // 拿到去支付的id
    const getCurrentGoods = async (id:number) => {
        const data = await RequestGoodsById(id)
        const { resultCode, resultInfo } = data
        if (resultCode === 1) {
            groupGoods.value = resultInfo
        }
    }
      // 控制tab默认页
      const active = ref(0)
    
    const router = useRouter()
    // 邀请好友，拼团成功，去待付款页面
    const useGroup = getcollageStatus()
    const goPayment = (id:number) => {
        history.back()
        Toast.success('拼团成功');
        const groupGoodsStatus : Ref<Array<ICartGoods>> = ref([])
        useGroup.saveCollageGoods(groupGoods)
        active.value = 3
    }

    // // 待付款页面商品数据
    // const usePaymentGoods = payment()
    // const goPaymentGoods = (id:number) => {
    //     history.back()
    //     Toast.success('拼团成功');
    //     const groupGoodsStatus : Ref<Array<ICartGoods>> = ref([])
    //     usePaymentGoods.saveCollageGoods(groupGoods)
    //     active.value = 3
    // }

    // 再次拼团
    const againCollage = (id:number) => {
        history.back()
        Toast.success('拼团成功');
        const groupGoodsStatus : Ref<Array<ICartGoods>> = ref([])
        useGroup.saveCollageGoods(groupGoods)
        active.value = 3
    }

     // 全选删除
     const bindDeleteAll = () => {
        useGroup.deleteAllGoods()
    }
  

    // 取消按钮
    const closeBtn = () => {
        history.back()
        // active.value = 3
    }

    // 刷新页面时，要自动运行组件
    onMounted(() => {
        currentGoods(id)
    })
    return { bindDeleteAll,useGroup,againCollage,goPayment,getCurrentGoods,onClickLeft, onviewDetails, onviewOrder, currentGoods,groupGoods,active,closeBtn }
}